//商品列表
var app = getApp(); //全局APP

Page({
	//页面的初始数据
	data: {
		operators: [],
		operatorIds: [],
		operatorIndex: 0,
		date: '',
		allNum: '总发放数:0套',
		num: '当日发放数:0套',
		listData: [],
		buttonDisabled: false, // 按钮是否可用
		btnExportDisabled: false
	},

	//加载执行
	onShow: function() {
		// 获取当前日期
		var today = new Date();
		today.setTime(today.getTime());
		var month = today.getMonth() + 1;
		var day = today.getDate();
		if (month < 10) month = "0" + month;
		if (day < 10) day = "0" + day;
		var time = today.getFullYear() + "-" + month + "-" + day;
		this.setData({
			btnExportDisabled: false,
			date: time
		});

		this.getApartmentMgrOperatorsInfo();
	},

	// 获取当前用户信息
	getApartmentMgrOperatorsInfo: function() {
		var page = this;

		var data = {
			platform: 2, // 平台id，标识是小程序登录的
		};

		// 提交请求
		app.api.getApartmentMgrOperatorsInfo(data, function(res) {
			if (res.status) {
				var tmp_operators = [];
				var tmp_operatorIds = [];

				for (var i = 0; i < res.data.length; i++) {
					tmp_operators[i] = res.data[i].truename;
					tmp_operatorIds[i] = res.data[i].id;
				}

				page.setData({
					operators: tmp_operators,
					operatorIds: tmp_operatorIds,
					operatorIndex: 0
				});

				page.sumbit();
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
			}
		});
	},

	// 绑定管理员
	bindOperatorChange: function(e) {
		this.setData({
			operatorIndex: e.detail.value
		});
	},

	// 绑定日期
	bindDateChange: function(e) {
		this.setData({
			date: e.detail.value
		});
	},

	// 查询
	sumbit: function() {
		var page = this;
		if (page.data.operatorIds[page.data.operatorIndex] == '') {
			app.common.errorToBack('请选择管理员!', 0);
			return false;
		}

		app.common.buttonDisabled(this);
		
		var operatorInfo = '';
		var all = 1;
		// 构造查询条件
		if (page.data.operatorIds[page.data.operatorIndex] == 0) {
			for (var i = 1; i < page.data.operatorIds.length; i++) {
				operatorInfo = operatorInfo + page.data.operatorIds[i] + "','";
			}

			operatorInfo = operatorInfo.substring(0, operatorInfo.length - 3);
		} else {
			all = 0;
			operatorInfo = page.data.operatorIds[page.data.operatorIndex];
		}

		var data = {
			operator: operatorInfo, // 管理员
			date: page.data.date, // 日期
			all: all,
			platform: 2 // 平台id，标识是小程序登录的 
		};

		// 提交请求
		app.api.getApartmentSuppliesInfo(data, function(res) {
			if (res.status) {
				page.setData({
					allNum: '总发放数:' + res.data.allNum + '套',
					num: '当日发放数:' + res.data.num + '套',
					listData: res.data.list
				});
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);

				page.setData({
					allNum: '总发放数:0套',
					num: '当日发放数:0套',
					listData: []
				});
			}
		});
	},
	
	// 导出
	exportData: function() {
		var page = this;
		if (page.data.operatorIds[page.data.operatorIndex] == '') {
			app.common.errorToBack('请选择管理员!', 0);
			return false;
		}

		app.common.buttonDisabled(this);
		
		var operatorInfo = '';
		var all = 1;
		// 构造查询条件
		if (page.data.operatorIds[page.data.operatorIndex] == 0) {
			for (var i = 1; i < page.data.operatorIds.length; i++) {
				operatorInfo = operatorInfo + page.data.operatorIds[i] + "','";
			}

			operatorInfo = operatorInfo.substring(0, operatorInfo.length - 3);
		} else {
			all = 0;
			operatorInfo = page.data.operatorIds[page.data.operatorIndex];
		}

		if(page.data.btnExportDisabled) {
			return;
		}
		
		page.setData({
			btnExportDisabled: true
		});
		
		var data = {
			operator: operatorInfo, // 管理员
			date: page.data.date, // 日期
			all: all,
			platform: 2 // 平台id，标识是小程序登录的 
		};

		// 提交请求
		app.api.apartmentExportDailyData(data, function(res) {
			if (res.status) {
				var strFileName = page.data.date + '日账单.xls'; //文件名带后缀
				const newPath = `${wx.env.USER_DATA_PATH}/${strFileName}`;
				page.deletContract();
				wx.downloadFile({
					url: res.url,
					filePath: newPath,
					success: function(res) {
						const filePath = res.tempFilePath;
						wx.openDocument({
							filePath: newPath,
							showMenu: true,
							fileType: 'xls',
							success: function(res) {}
						})
					},
					fail: function(res) {
						wx.hideLoading();
					}
				})
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
			}
			 
			page.setData({
				btnExportDisabled: false
			});
		});
	},
	 
	// 删除本地文件
	deletContract() {
		try {
			let file = wx.getFileSystemManager();
			file.readdir({
				dirPath: `${wx.env.USER_DATA_PATH}`,
				success: res => {
					if (res.files.length > 2) {
						file.unlink({
							filePath: `${wx.env.USER_DATA_PATH}/${res.files[0]}`,
							complete: res => {
	
							}
						})
					}
				}
			})
		} catch (error) {
	
		}
	},
	
});
